/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.adapter.viewholder;

import com.oushangfeng.pinneddemo.ResourceTable;
import com.oushangfeng.pinneddemo.adapter.CommonProvider;
import com.oushangfeng.pinneddemo.adapter.ViewHolder;
import com.oushangfeng.pinneddemo.datainterface.EventTransmissionListener;
import com.oushangfeng.pinneddemo.model.CommonViewModel;
import com.oushangfeng.pinneddemo.util.DensityUtils;
import com.oushangfeng.pinneddemo.util.ToastUtil;
import ohos.agp.components.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * CommonViewHolder
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 */
public class CommonViewHolder extends ViewHolder<CommonViewModel> {
    private static final int INT_2 = 2;
    private static final int INT_3 = 3;
    private static final int INT_10 = 10;
    private static final int INT_20 = 20;
    private static final int INT_100 = 100;
    private static final float FLOAT_20 = 20f;

    private Image mImgItemNsModel;
    private Text mTextItemNumber;
    private DependentLayout mDplItemAll;
    private Random random = new Random();
    private final int mMRANDOMNUM = 130;
    private final int mCOMWIDTH = 80;
    private int itemHeight = getWaterfallItemWidth() / INT_3;
    private ToastUtil toastUtil;
    /**
     * CommonViewHolder
     *
     * @param eventTransmissionListener  点击
     * @param component view
     * @param provider pr
     * @param componentContainer container
     */
    public CommonViewHolder(EventTransmissionListener eventTransmissionListener,
                            Component component, CommonProvider provider, ComponentContainer componentContainer) {
        super(eventTransmissionListener, component, provider, componentContainer);
        mImgItemNsModel = (Image) findComponentById(ResourceTable.Id_img_item_ns_model);
        mTextItemNumber = (Text) findComponentById(ResourceTable.Id_text_item_number);
        mDplItemAll = (DependentLayout) findComponentById(ResourceTable.Id_dpl_item_all);
        toastUtil = new ToastUtil(getContext());
    }

    /**
     * onDataBound
     */
    @Override
    public void onDataBound() {
        // 序号
        mTextItemNumber.setText(getModel().getName());

        // 布局
        mDplItemAll.setWidth(getWidth());

        // 图片
        mImgItemNsModel.setPixelMap(getModel().getImgId());
        mImgItemNsModel.setScaleMode(Image.ScaleMode.STRETCH);

        int randomWidth = random.nextInt(mMRANDOMNUM);
        if (randomWidth < mCOMWIDTH) {
            randomWidth = mCOMWIDTH;
        }

        // 0：线性  1: 网格   2:瀑布
        if (!"".equals(getModel().getLayout())) {
            switch (getModel().getLayout()) {
                case 1:
                    break;
                case INT_2:
                    mDplItemAll.setHeight(DensityUtils.fpToPx(getModel().getContext(), randomWidth));
                    mDplItemAll.setMarginTop(DensityUtils.fpToPx(getModel().getContext(), intRandom(0, getPosition())));
                    mDplItemAll.setMarginBottom(
                            DensityUtils.fpToPx(getModel().getContext(), intRandom(0, getPosition())));
                    mImgItemNsModel.setPaddingTop(DensityUtils.fpToPx(getModel().getContext(), FLOAT_20));
                    break;
                default:
                    break;
            }
        }

        // 单击
        mDplItemAll.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toastUtil.toast(getContext(), "item click :" + getModel().getTypeName()
                        + ",position " + getModel().getItemValue() + ",id " + getPosition());
            }
        });

        // 双击
        mDplItemAll.setDoubleClickedListener(new Component.DoubleClickedListener() {
            @Override
            public void onDoubleClick(Component component) {
                toastUtil.toast(getContext(), "item click :" + getModel().getTypeName()
                        + ",position " + getModel().getItemValue() + ",id " + getPosition());
            }
        });
    }

    /**
     * 多级、网格布局使用
     *
     * @return dplWith
     */
    private int getWidth() {
        int dplWith = DensityUtils.getDisplayWidth(getModel().getContext()) / INT_3;
        return dplWith;
    }

    /**
     * 瀑布流随机大小
     *
     * @return wiwHeight
     */
    private int getWaterfallItemWidth() {
        int gni = intRandom(INT_10, INT_20);
        int wiwHeight = INT_100 + gni;
        return wiwHeight;
    }

    /**
     * 随机数
     *
     * @param min 最小数据
     * @param max 最大数据
     * @return dla 返回随机数据
     */
    private int intRandom(int min, int max) {
        // 获取当前时间，HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        long dl = dateToLong(date);

        while (dl > max) {
            dl = dl / INT_2;
            if (dl < min) {
                dl = min + dl % min;
                break;
            }
            if (itemHeight < dl) {
                dl = itemHeight - max;
            }
        }
        int dla = (int) dl;
        return dla;
    }

    /**
     * dateToLong
     *
     * @param date 时间
     * @return getTime
     */
    private long dateToLong(Date date) {
        return date.getTime();
    }
}
